package com.training.dao;

import com.training.model.User;
import com.training.util.JdbcTemplate;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Map;

public class UserDao {
    private final JdbcTemplate jdbcTemplate = new JdbcTemplate();

    // 根据用户名查找用户
    public User findByUsername(String username) throws SQLException {
        String sql = "SELECT id, username, password, real_name, created_at, updated_at FROM users WHERE username = ?";
        
        Map<String, Object> result = jdbcTemplate.queryForObject(sql, username);
        if (result != null) {
            return mapToUser(result);
        }
        return null;
    }

    // 根据ID查找用户
    public User findById(long id) throws SQLException {
        String sql = "SELECT id, username, password, real_name, created_at, updated_at FROM users WHERE id = ?";
        
        Map<String, Object> result = jdbcTemplate.queryForObject(sql, id);
        if (result != null) {
            return mapToUser(result);
        }
        return null;
    }

    // 检查用户名是否存在
    public boolean existsByUsername(String username) throws SQLException {
        String sql = "SELECT COUNT(*) FROM users WHERE username = ?";
        Object result = jdbcTemplate.queryForValue(sql, username);
        return ((Number) result).intValue() > 0;
    }

    // 创建新用户
    public User create(String username, String password, String realName) throws SQLException {
        String sql = "INSERT INTO users (username, password, real_name) VALUES (?, ?, ?)";
        int generatedId = jdbcTemplate.insertAndGetKey(sql, username, password, realName);
        return findById(generatedId);
    }

    // 更新用户信息
    public boolean update(long id, String username, String password, String realName) throws SQLException {
        String sql = "UPDATE users SET username = ?, password = ?, real_name = ? WHERE id = ?";
        int rowsAffected = jdbcTemplate.update(sql, username, password, realName, id);
        return rowsAffected > 0;
    }

    // 删除用户
    public boolean deleteById(long id) throws SQLException {
        String sql = "DELETE FROM users WHERE id = ?";
        int rowsAffected = jdbcTemplate.update(sql, id);
        return rowsAffected > 0;
    }

    // 检查用户是否存在
    public boolean existsById(long id) throws SQLException {
        String sql = "SELECT COUNT(*) FROM users WHERE id = ?";
        Object result = jdbcTemplate.queryForValue(sql, id);
        return ((Number) result).intValue() > 0;
    }

    // 映射结果集到用户对象
    private User mapToUser(Map<String, Object> result) {
        User user = new User();
        user.setId(((Number) result.get("id")).longValue());
        user.setUsername((String) result.get("username"));
        user.setPassword((String) result.get("password"));
        user.setRealName((String) result.get("real_name"));
        
        Timestamp createdAt = (Timestamp) result.get("created_at");
        if (createdAt != null) {
            user.setCreatedAt(createdAt.toLocalDateTime());
        }
        
        Timestamp updatedAt = (Timestamp) result.get("updated_at");
        if (updatedAt != null) {
            user.setUpdatedAt(updatedAt.toLocalDateTime());
        }
        
        return user;
    }
}
